<?php session_start();echo $_SESSION['id_Exp'];$_SESSION['EnQues'];  ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <?php     $cntr=0;
      //  $answers[]=$_SESSION['answer'];
     $q= $_POST['2'];
      // echo "<script>alert ('$q'); </script>";
      
        //echo "<script>alert('$answers[1]');</script>";
        $con= mysql_connect("localhost","root","");
        if(!$con){
die('Connection Error!:' . mysql_error());
}
mysql_select_db ("usabilityevaluation",$con);
//set the experiment ID;

$exp_id=0;
$exp_id=$_SESSION['id_Exp'];
// here it chose beetween two Submit Button;
if ($_POST['tasks'] == 'Finish and Show Saved entries') {
//session_destroy();
 mysql_close($con);
header("Location:expload.php");
}


else {
$title=$_POST['questionName'];
$type=$_POST['D1'];
$comment=$_POST["comment"];
$exp_id=$_SESSION['id_Exp'];
$id_task=$_SESSION['id_task'];
//retrieve answers
if ($type =='Multiple Choice'){
$ans1=$_POST['1'];$ans2=$_POST['2'];$ans3=$_POST['3'];$ans4=$_POST['4'];
}
else if($type=='Text to Fill'){$ans1=$_POST['1'];}
else {$ans1='true';$ans2='false';}

//echo "<script>alert('".$title.$type.$comment.$id_task.$exp_id."');</script>";
//Select Max Question;
$question_id=0;
$result3 = mysql_query("SELECT MAX(n_question) FROM question WHERE id_exp='$exp_id' and id_task='$id_task'") ;
$num_results3 = mysql_num_rows($result3);
while($row3 = mysql_fetch_array($result3)){
if ($num_results3 ==null){$question_id=1;}
else {$question_id=$row3[0] + 1;}
}

// INsert the question;

$query2="INSERT INTO question (n_question,Title,Type,Comment,id_task,id_exp) VALUES ($question_id,'$title','$type','$comment',$id_task,$exp_id)";

@mysql_query($query2) or die('Error, insert query jjjnbsakjdkj failed');
//Insert question;
//retreive question
$queryQ="select id_question from question where n_question=$question_id and id_task=$id_task and id_exp=$exp_id";
$result333=mysql_query($queryQ) or die('Error, insert query question failed');
while($row333 = mysql_fetch_array($result333)){
$id_question=$row333[0];
}

if ($type=='Multiple Choice'){
$query23="INSERT INTO answers (number,id_question,title) VALUES (1,$id_question,'$ans1')";
$query24="INSERT INTO answers (number,id_question,title) VALUES (2,$id_question,'$ans2')";
$query25="INSERT INTO answers (number,id_question,title) VALUES (3,$id_question,'$ans3')";
$query26="INSERT INTO answers (number,id_question,title) VALUES (4,$id_question,'$ans4')";
mysql_query($query23) or die('Error, insert query answer failed'); mysql_query($query24) or die('Error, insert query failed'); mysql_query($query25) or die('Error, insert query failed'); 
mysql_query($query26) or die('Error, insert query  failed'); 
}
else if ($type=='TrueFalse'){
$query123="INSERT INTO answers (number,id_question,title) VALUES (1,$id_question,'True')";
$query124="INSERT INTO answers (number,id_question,title) VALUES (2,$id_question,'False')";mysql_query($query123) or die('Error, insert query failed'); 
mysql_query($query124) or die('Error, insert query failed'); 
}
else {
$query223="INSERT INTO answers (number,id_question,title) VALUES (1,$id_question,'')";
mysql_query($query223) or die('Error, insert query failed');
}

mysql_close($con);
 header("Location: {$_SERVER['HTTP_REFERER']}"); 
 }
 mysql_close($con);
 echo " the insert was done."; ?>
       
        
    </body>
</html>
